package com.cw.common.method;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.cw.vo.QueueNumberVO;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * @author xnd
 * @since 2024/4/16 9:36
 */
public class QueueNumberMsg {

    public static QueueNumberVO getQueueNum(String areaId) throws IOException {
        JSONObject resDataOb = new JSONObject();
        Integer code = null;
        String msg = null;
        QueueNumberVO queueNumberVO = new QueueNumberVO();
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        try {
            // 创建客户端连接对象
            httpClient = HttpClients.createDefault();
            // 构建Get请求对象
            HttpGet httpGet = new HttpGet("http://172.16.2.118:9000/zyQueue/getQueueNum?areaId="+areaId);
            //增加header
            httpGet.addHeader("Content-Type", "application/x-www-form-urlencoded");
            // 设置超时时间，其中connectionRequestTimout（从连接池获取连接的超时时间）、connetionTimeout（客户端和服务器建立连接的超时时间）、socketTimeout
            // （客户端从服务器读取数据的超时时间），单位都是毫秒
            RequestConfig config = RequestConfig.custom()
                    .setConnectTimeout(10000)
                    .setConnectionRequestTimeout(3000)
                    .setSocketTimeout(20000)
                    .build();
            httpGet.setConfig(config);
            // 获取返回对象
            response = httpClient.execute(httpGet);
            // 整理返回值
            HttpEntity entity = response.getEntity();
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                String result = EntityUtils.toString(entity, "utf-8");
                resDataOb = JSONUtil.toBean(result, JSONObject.class);
                if (!resDataOb.isEmpty()){
                    code = (Integer) resDataOb.get("code");
                    msg = (String) resDataOb.get("msg");
                    Object data =  resDataOb.get("data");
                    JSONObject vo = JSONUtil.toBean(data.toString(), JSONObject.class);
                    String number = (String) vo.get("number");
                    Integer waitNumber = (Integer) vo.get("waitNumber");
                    queueNumberVO.setResponseType(code);
                    queueNumberVO.setResponseMsg(msg);
                    queueNumberVO.setNumber(number);
                    queueNumberVO.setWaitNumber(waitNumber);
                }
            } else {
                code = (Integer) resDataOb.get("code");
                msg = (String) resDataOb.get("msg");
                queueNumberVO.setResponseType(code);
                queueNumberVO.setResponseMsg(msg);
            }
            return queueNumberVO;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            // 关闭连接和流
            try {
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
